package com.wangshuo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangshuo.config.Result;
import com.wangshuo.config.ResultCodeEnum;
import com.wangshuo.entity.Goods;
import com.wangshuo.entity.Type;
import com.wangshuo.service.GoodsService;
import com.wangshuo.service.TypeService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
public class TypeController {
    @Resource
    private TypeService typeService;
    @Resource
    private GoodsService goodsService;
    @GetMapping("/list")
    public Result list(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String keyword){
        IPage<Type> page=new Page<>(pageNum,pageSize);
        IPage<Type> list=typeService.getlist(page,keyword);
        return Result.ok(list);
    }
    @PostMapping("/add")
    public Result add(@RequestBody Type type){
        typeService.saveOrUpdate(type);
        return Result.ok("成功");
    }
    @GetMapping("/delete")
    public Result delete(@RequestParam Integer id){
        LambdaQueryWrapper<Goods> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Goods::getTypeid,id);
        Long count=goodsService.count(queryWrapper);
        if(count>0){
            return Result.build(500, ResultCodeEnum.TypeHasGood);
        }else{
            typeService.removeById(id);
            return Result.ok("成功");

        }
    }
    @GetMapping("/getall")
    public Result getall(){
        List<Type> list=typeService.list();
        return Result.ok(list);
    }

}
